import 'package:animated_search_bar/animated_search_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_deer/game/iview/game_search_list_iview.dart';
import 'package:flutter_deer/game/models/game_item_entity.dart';
import 'package:flutter_deer/game/presenter/game_search_list_presenter.dart';
import 'package:flutter_deer/game/provider/game_search_list_provider.dart';
import 'package:flutter_deer/home/webview_page.dart';
import 'package:flutter_deer/index/iview/banner_detail_iview.dart';
import 'package:flutter_deer/index/models/banner_entity.dart';
import 'package:flutter_deer/index/presenter/banner_detail_persenter.dart';
import 'package:flutter_deer/index/provider/banner_detail_Provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/util/image_utils.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/util/theme_utils.dart';
import 'package:flutter_deer/widgets/fadein_text.dart';
import 'package:flutter_deer/widgets/load_image.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/widgets/my_card.dart';
import 'package:flutter_deer/widgets/my_refresh_list.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_deer/util/theme_utils.dart';
import 'package:webview_flutter/webview_flutter.dart';

class BannerDetailPage extends StatefulWidget {

  const BannerDetailPage({super.key,required this.id});

  final String? id;

  @override
  _BannerDetailPageState createState() => _BannerDetailPageState();
}

class _BannerDetailPageState extends State<BannerDetailPage> with SingleTickerProviderStateMixin,BasePageMixin<BannerDetailPage, PowerPresenter>, AutomaticKeepAliveClientMixin implements BannerDetailIMvpView {

  final GlobalKey _bodyKey = GlobalKey();
  late BannerDetailPagePresenter bannerDetailPagePresenter;
  BannerDetailProvider provider = BannerDetailProvider();
  WebViewController? webViewController = new WebViewController();

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    bannerDetailPagePresenter = new BannerDetailPagePresenter();
    powerPresenter.requestPresenter([bannerDetailPagePresenter]);
    return powerPresenter;
  }

  @override
  void setBannerDetail(FocusMapDetailEntity? detailEntity){
    provider.setBannerDetailEntity(detailEntity);
    if(detailEntity!.viewType==1) {
      webViewController!.loadHtmlString(detailEntity!.content.nullSafe);
    }
  }

  @override
  void initState() {
    super.initState();
    if(mounted){
      bannerDetailPagePresenter.getBannerDetail(widget.id.nullSafe);
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    final Color? iconColor = ThemeUtils.getIconColor(context);
    return ChangeNotifierProvider<BannerDetailProvider>(
        create: (_) => provider,
        child:  Consumer<BannerDetailProvider>(
        builder: (_,provider,child){
          return Scaffold(
              key: _bodyKey,
              appBar: MyAppBar(centerTitle: provider.stateType==StateType.loading?'':'${provider.detail!.title.nullSafe}'),
              body: provider.stateType==StateType.loading?StateLayout(type: StateType.loading):(provider.detail!.viewType==2?WebViewPage(displayTitle:false,title: provider.detail!.title.nullSafe, url: provider.detail!.link.nullSafe):WebViewWidget(controller: webViewController!,)),
          );
        }
    )
    );
  }

  @override
  bool get wantKeepAlive => false;
}
